<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class baseController extends MY_Controller{
	public function __construct(){
		parent::__construct();	
        $this->load->library('session');
        $this->load->helper('text');
        
        $this->load->model("category_Model","categoryModel", true);
        $this->load->model("entry_Model","entryModel", true);
        $this->load->model("account_Model","accountModel", true);
        $this->load->model("role_Model","roleModel", true);
        $this->load->model("comment_Model","commentModel", true);
        $this->load->model("question_Model","questionModel", true);
        
        $this->_data['mostview'] = $this->getMostView();
        $this->_data['categories'] = $this->getCategories();
        $this->_data['slugcategories'] = $this->getSlugCategories();
        $this->_data['slugparent'] = $this->getSlugParent();
        $this->_data['newquestions'] = $this->getNewQuestion();
        $this->_data['newentries'] = $this->getNewEntryForEachCategory();
        $this->_data['newcomments'] = $this->getNewComment();
	}
	
    public function getMostView() {
        return $this->entryModel->get_order_by(7, 'views', 'desc');
    }
    
    public function getCategories() {
        return $this->categoryModel->get_order_where(null, 'number', 'asc', array('parentid' => 0));
    }
    
    public function getSlugCategories() {
        $result = array();
        $categories = $this->categoryModel->get();
        foreach($categories as $k=>$v) {
            $result[$v['id']] = $v['slug'];
        }
        return $result;
    }
    
    public function getSlugParent() {
        $result = array();
        $slugs = $this->getSlugCategories();
        $categories = $this->categoryModel->get();
        foreach($categories as $k=>$v) {
            if($v['parentid'] != 0) {
                $result[$v['id']] = $slugs[$v['parentid']];
            }
        }
        return $result;
    }
    
    public function getNewQuestion() {
        return $this->questionModel->get_order_by(10, 'id', 'desc');
    }
    
    public function getNewComment() {
        $result = array();        
        $comments = $this->commentModel->get_order_by(7, 'id', 'desc');
        foreach($comments as $k=>$v) {
            if(!isset($accounts[$v['accountid']])) {
                $acc = $this->accountModel->read($v['accountid']);
                $accounts[$v['accountid']] = $acc['username'];
            }
            $v['accountid'] = $accounts[$v['accountid']];
            
            if($v['entryid']) {
                $ent = $this->entryModel->read($v['entryid']);                
                $v['entryid'] = $ent['title'];
                $v['slug'] = $ent['slug'];
                $v['categoryid'] = $ent['categoryid'];
            } 
            elseif($v['questionid']) {
                $que = $this->questionModel->read($v['questionid']);
                $v['questionid'] = $que['title'];
                $v['slug'] = $que['slug'];
            } else {
                continue;
            }
            $result[] = $v;
        }
        return $result;
    }
    
    public function getNewEntryForEachCategory($limit = 8) {
        $result = array();
        $categories = $this->getCategories();
        foreach($categories as $k=>$v) {
            $entries = $this->getNewEntry($v['id'], $limit);
            if(count($entries)) $result[$v['categoryname']] = $entries;
        }
        return $result;
    }
    
    public function getNewEntry($categoryid, $limit) {
        return $this->entryModel->get_order_where($limit, 'id', 'desc', array('categoryid' => $categoryid));
    }
    
    public function getOlderEntry($entry, $limit) {
        return $this->entryModel->get_order_where($limit, 'id', 'desc', array('categoryid' => $entry['categoryid'], 'id < ' => $entry['id']));
    }
    
	public function loadView($data = null){
		$this->load->view('templates',$data);
	}
    
	public function loadStatic($template,$data = null){
		return $this->load->view($template,$data,TRUE);
	}
}
